<?php
require_once(dirname(__FILE__).'/Abstract.php');
class Status extends AbstractConsultationController 
{
	public function indexAction()
	{
		$this->listingAction();
	}
    
	public function listingAction()
	{
        $d = $this->initListing('ORM/Consultation/ConsultationStatusORM', 'id_status', 'medic_consultation_status', 'medic_consultation_status_lang');
        
        $d['menu'] = 'consultation';
        $d['page_name'] = 'consultation_status';
        $d['title_for_layout'] = $this->l('List of consultation status');
        
        $this->load->view('Templates/Layouts/Admin/header', $d);
        $this->load->view('Cpannel/Consultation/Status/Listing', $d);
        $this->load->view('Templates/Layouts/Admin/footer', $d);
	}
    
    public function editAction()
    {
        $this->load->model('ORM/Consultation/ConsultationStatusORM');
        $d['id_status'] = (int)$this->input->get_post('id');
        
        // Init the form structure
        $d['form'] = $this->initForm($d['id_status']);
        
        // The form was submitted
        if ($this->input->post('btn_submit') !== false) {
            $config = $this->initValidation($d['form']);
            $this->form_validation->set_rules($config);
            
            // Process form
            if ($this->form_validation->run() == FALSE ){
				$this->session->set_flashdata('errorMessage', $this->l('An error occured. Please check filled data and try again.'));
            } else {
                $record = $this->extractSubmittedValues($d['form']);
                if ($this->input->get_post('id')) { 
                    $record['id_status'] = (int)$this->input->get_post('id');
                }
                // Save submitted values
                if ($this->ConsultationStatusORM->save($record)) {
                    $this->session->set_flashdata('successMessage', $this->l('Item successfully saved.'));
                    
                    // Redirect to the sign in form
    				redirect('Cpannel/Consultation/Status');
                    die();
                } else {
                    $this->session->set_flashdata('errorMessage', $this->l('An error occured while saving. Please try again later.'));
                }
            }
        }
        
        if ($d['id_status']) {
            $d['item'] = $this->ConsultationStatusORM->getById($d['id_status']);
            if (!empty($d['item']->id_status)) {
                $d['id']   = (int)$d['id_status'];
            } else {
                $d['id_status'] = false;
                unset($d['item']);
                $this->session->set_flashdata('errorMessage', $this->l('The target account was not found.'));
            }
        }
        
        $d['menu'] = 'consultation';
        $d['page_name'] = 'consultation_status';
        $d['title_for_layout'] = $d['id_status'] ? sprintf($this->l('Update consultation status #%s'), str_pad($d['id_status'], 6, '0', STR_PAD_LEFT)) : $this->l('Add a new consultations status');
        $d['languages'] = NZ_Config::$languages;
        
        $this->load->view('Templates/Layouts/Admin/header', $d);
        $this->load->view('Templates/FormHelper/editHelper', $d);
        $this->load->view('Templates/Layouts/Admin/footer', $d);
    }
    
    public function addAction()
    {
        $this->editAction();
    }
    
    public function deleteAction()
    {
        $this->load->model('ORM/Consultation/ConsultationStatusORM');
        
        if ($id = (int)$this->input->get_post('id')) {
            $obj = $this->ConsultationStatusORM->getById($id);
            
            if ($obj->id_status) {
                if ($this->ConsultationStatusORM->removeByIdStatus((int)$obj->id_status)) {
                    $this->session->set_flashdata('successMessage', $this->l('Item successfully deleted.'));
                } else {
                    $this->session->set_flashdata('errorMessage', $this->l('An error occured while deleting update. Please try agin later.'));
                }
            } else {
                $this->session->set_flashdata('errorMessage', $this->l('The target account was not found.'));
            }
                
        } else {
            $this->session->set_flashdata('errorMessage', $this->l('An error occured. Bad HTTP request.'));
        }
        
        redirect('Cpannel/Consultation/Status');
        die();
    }
    
    public function deleteBulkAction()
    {
        $this->deleteAction();
    }
    
    protected function initForm ($id = null) 
    {
        return array(
            // Each section
            array(
                // Section configs
                'legend' => array(
                    'title' => $id ? sprintf($this->l('Update status #%s'), str_pad($id, 6, '0', STR_PAD_LEFT)) : $this->l('Add a new status'),
                ),
                // In case of tabs, each tabs. The structure of a table is the same of a section, except tabs option.
                'tabs' => array(
                    // array('tabConfig' => array('id' => '', 'title' => ''), sections)
                ),
                // In case input, each input
                'input' => array(
    				array(
    					'type'     => 'text',
    					'label'    => $this->l('Status name :'),
    					'desc'     => $this->l('The name of the consultation status. E.g. : Started'),
    					'name'     => 'label',
                        'rules'    => 'trim|required|min_length[4]|xss_clean',
                        'size'     => 20,
                        'lang'     => true,
                        'required' => true,
    				),
    				array(
    					'type'     => 'textarea',
    					'label'    => $this->l('Description :'),
    					'desc'     => $this->l('More information about this consultation status'),
    					'name'     => 'description',
                        'rules'    => 'trim|required|max_length[255]|xss_clean',
                        'size'     => 20,
                        'lang'     => true,
                        'required' => true,
    				),
                ),
            ),
            'formSettings' => array(
                'action' => base_url('Cpannel/Consultation/Status/edit'.($id ? ('?id='.(int)$id) : '')),
                'multiSection' => false,
            ),
            'actionBtn' => array(
                'submit' => array(
                    'title' => $this->l('Save'),
                ),
                'cancel' => array(
                    'title' => $this->l('Cancel'),
                    'href' => base_url('Cpannel/Consultation/Status'),
                )
            )
        );
    }
}

